const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require('../../libs/regenerator');
const co = require("../../libs/co");
const http = require('../../libs/httpRequest');
const appConfig = require('../../appConfig');

var ProductService = function () {
   var _getCategoryList = function *() {
     var ret = yield http.request({
         url: `${appConfig.api.baseUrl}/category/list`,
         data: {},
         method: 'POST',
         success: (res) => {
             console.log(res.data);
         },
         fail: (err) => {
             console.log(err);
         }
     });
     return ret;
   }

   var _getProductsByCategory = function *(categoryId) {
     let ret = yield http.request({
         url: `${appConfig.api.baseUrl}/product/list`,
         data: {categorys:categoryId},
         method: 'POST',
         success: (res) => {
             console.log(res.data);
         },
         fail: (err) => {
             console.log(err);
         }
     });
     return ret;
   }

   var _searchProductResult = function *(keyName) {
     let ret = yield http.request({
         url: `${appConfig.api.baseUrl}/product/list`,
         data: {keyName:keyName,categorys:"",brands:""},
         method: 'POST',
         success: (res) => {
             console.log(res.data);
         },
         fail: (err) => {
             console.log(err);
         }
     });
     return ret;
   }

   var _getProductInfo = function *(productId) {
     let ret = yield http.request({
         url: `${appConfig.api.baseUrl}/product/info`,
         data: {productId:productId},
         method: 'POST',
         success: (res) => {
             console.log(res.data);
         },
         fail: (err) => {
             console.log(err);
         }
     });
     return ret;
   }

   let _addFavorite = function *(productId,skuId) {
     let ret = yield http.request({
         url: `${appConfig.api.baseUrl}/favorite/add`,
         data: {productId:productId,sId:skuId},
         method: 'POST',
         success: (res) => {
             console.log(res.data);
         },
         fail: (err) => {
             console.log(err);
         }
     });
     return ret.apiStatus.statusCode == 0 ? true : false;
   }

   let _delFavorite = function *(productId,skuId) {
     let ret = yield http.request({
         url: `${appConfig.api.baseUrl}/favorite/del`,
         data: {productId:productId,sId:skuId},
         method: 'POST',
         success: (res) => {
             console.log(res.data);
         },
         fail: (err) => {
             console.log(err);
         }
     });
     return ret.apiStatus.statusCode == 0 ? true : false;
   }

   var _getFavorite = function *(pageIndex,pageSize) {
     let ret = yield http.request({
         url: `${appConfig.api.baseUrl}/favorite/list`,
         data: {pageIndex:pageIndex,pageSize:pageSize},
         method: 'POST',
         success: (res) => {
             console.log(res.data);
         },
         fail: (err) => {
             console.log(err);
         }
     });
     return ret;
   }

   return {
     getCategoryList : _getCategoryList,
     getProductsByCategory: _getProductsByCategory,
     searchProductResult : _searchProductResult,
     getProductInfo : _getProductInfo,
     addFavorite : _addFavorite,
     delFavorite : _delFavorite,
     getFavorite : _getFavorite
   }
}();

module.exports = ProductService;
